<template>
  <el-dialog
    title="规范下发"
    :visible.sync="dialogVisible"
    width="1000px">
    <el-table
      :data="normData"
      border
      ref="singleTable"
      highlight-current-row
      :header-cell-style="{background:'#17b3a3',color:'#FFFFFF'}"
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        prop="normid"
        header-align="center"
        align="center"
        label="规范编码">
      </el-table-column>
      <el-table-column
        prop="normdescribr"
        header-align="center"
        align="center"
        label="规范描述">
      </el-table-column>
      <el-table-column
        prop="curcentral"
        header-align="center"
        align="center"
        label="电流中心值">
      </el-table-column>
      <el-table-column
        prop="curdown"
        header-align="center"
        align="center"
        label="电流下限偏离">
      </el-table-column>
      <el-table-column
        prop="curup"
        header-align="center"
        align="center"
        label="电流上限偏离">
      </el-table-column>
      <el-table-column
        prop="volcentral"
        header-align="center"
        align="center"
        label="电压中心值">
      </el-table-column>
      <el-table-column
        prop="voldown"
        header-align="center"
        align="center"
        label="电压下限偏离">
      </el-table-column>
      <el-table-column
        prop="volup"
        header-align="center"
        align="center"
        label="电压上限偏离">
      </el-table-column>
    </el-table>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-date-picker
        v-model="value1"
        value-format="yyyy-MM-dd"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期">
      </el-date-picker>
      <el-form-item>
        <el-input v-model="dataForm.wmId" placeholder="焊机编码" clearable></el-input>
      </el-form-item>
<!--      <el-form-item>-->
<!--        <el-input v-model="dataForm.wmNote" placeholder="焊机描述" clearable></el-input>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button @click="getMachine()">查询</el-button>
        <el-button @click="confirmBinding()">确认绑定</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="machineData"
      border
      ref="downTable"
      highlight-current-row
      :header-cell-style="{background:'#17b3a3',color:'#FFFFFF'}"
      @selection-change="selectionChangeHandle"
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="wmid"
        header-align="center"
        align="center"
        label="焊机编号">
      </el-table-column>
      <el-table-column
        prop="wmnote"
        header-align="center"
        align="center"
        label="焊机描述">
      </el-table-column>
      <el-table-column
        prop="wmbrand"
        header-align="center"
        align="center"
        label="焊机品牌">
      </el-table-column>
      <el-table-column
        prop="wmmodel"
        header-align="center"
        align="center"
        label="焊机型号">
      </el-table-column>
      <el-table-column
        prop="wmtype"
        header-align="center"
        align="center"
        label="焊机类型">
      </el-table-column>
      <el-table-column
        prop="colmodel"
        header-align="center"
        align="center"
        label="采集模块编码">
      </el-table-column>
      <el-table-column
        prop="nexttime"
        header-align="center"
        align="center"
        label="下次维护时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.wmid)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.wmid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script>
  export default {
    data(){
      return {
        dataForm: {
          key: '',
          wmId: '',
          wmNote: ''
        },
        normData: [],
        machineData: [],
        datajson: [],
        dataList: [],
        value1: '',
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        Visiable: false,
        dialogVisible:false,
        aa: false,
        area: {
          area1: ''
        }
      }
    },
    activated(){
      this.getMachine();
    },
    mounted () {
      this.getMachine();
    },
    methods: {
      init(row){
        this.dialogVisible = true;
        this.normData = Array(row);
        console.log(this.normData)
        // console.log(this.machineData)
        // console.log('子组件normid为' + row.normid)
        // console.log('normdata的normid为' + this.normData.normid)
        // console.log('子组件信息为' + this.normData)
      },
      // 确认绑定
      confirmBinding(){
        console.log("当前规范编码："+this.$refs.singleTable.data[0].normid)
        console.log("规范开始时间："+this.value1[0]);
        console.log("规范开始时间："+this.value1[1]);
        console.log("焊机编号："+this.dataListSelections[0].wmid)
        this.$http({
          url: this.$http.adornUrl('/bindinfo/bindinfo/getbindinfo'),
          method: 'get',
          params: this.$http.adornParams({
            'wmId': this.dataForm.wmId,
            'normId': this.dataForm.normId
          })
        }).then(({data}) => {
          if (data) {
            console.log(data)
            var wmids = data.map(item => {
              return item.wmid;
            })
            console.log(wmids)
            if(wmids.includes(this.dataListSelections[0].wmid)){
              console.log("该焊机已绑定其他规范")
              this.$message.error('该焊机已绑定其他规范!');
            } else {
              console.log("成功！")
              this.$http({
                url: this.$http.adornUrl('/bindinfo/bindinfo/add'),
                method: 'post',
                params: this.$http.adornParams({
                  'wmId': this.dataListSelections[0].wmid,
                  'normId': this.$refs.singleTable.data[0].normid,
                  'startTime': this.value1[0],
                  'endTime': this.value1[1]
                })
              }).then(res => {
                console.log(res.status)
                if (res.status && res.status === 200) {
                  this.$message({
                    message: '绑定成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.getMachine()
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          }
        })
      },
      // 重置按钮
      resetSelectForm(){
        this.dataForm.wmId = '';
        this.dataForm.wmNote = '';
        this.getWmInfo();
      },
      // 获取数据列表
      getMachine () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/weldingmachineinfo/weldingmachineinfo/getwminfo'),
          method: 'get',
          params: this.$http.adornParams({
            'wmId': this.dataForm.wmId,
            'wmNote': this.dataForm.wmNote
          })
        }).then(({data}) => {
          if (data) {
            this.machineData = data
          } else {
            this.machineData = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.wmid
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/weldingmachineinfo/weldingmachineinfo/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      open() {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    }
  }
</script>
<<style lang="scss">
.el-table {
  margin-bottom: 20px;
}
.tr-color-0 {
  background: #f0f0f0;
}
.tr-color-1 {
  background: #fff;
}
.el-table .success-row {
  background: #DFEAF5;
}
</style>
